﻿// (c) Copyright Michael Antonio.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved
using System;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Input;
using Subsonic.Data;
using MichMan.Utilities.Requests;
using MichMan.Utilities;

namespace Subsonic.ViewModels
{
    public class AlbumListViewModel : PivotViewModel
    {
        public AlbumListViewModel(AlbumListType albumListType, string albumId)
        {
            AlbumListType = albumListType;
            Id = albumId;
        }

        public AlbumListType AlbumListType { get; private set; }

        public override IRequest LoadPivots()
        {
            GetAlbumListRequest getAlbumLisRequest = new GetAlbumListRequest(AlbumListType);
            getAlbumLisRequest.RunRequestCompleted += new RunRequestCompletedEventHandler(albumListRequest_RunRequestCompleted);
            getAlbumLisRequest.RunRequestAsync();
            return getAlbumLisRequest;
        }

        void albumListRequest_RunRequestCompleted(object sender, MichMan.Utilities.Requests.RunRequestCompletedEventArgs e)
        {
            GetAlbumListRequest request = (GetAlbumListRequest)sender;
            if (request.Response == null || request.RequestStatus != RequestStatus.Succeeded)
            {
                return;
            }

            foreach (var album in request.Response.AlbumList.Pivot(a => a.Id == Id))
            {
                Pivots.Add(new AlbumViewModel(album));
            }
        }
    }
}
